package com.sz.app.oss.api.common.project;

import com.sz.app.oss.constants.MessagesCodes;
import com.sz.biz.app.web.BaseController;
import com.sz.biz.common.constants.ErrorCodes;
import com.sz.biz.common.project.dto.TrdProjectKpiDto;
import com.sz.biz.common.project.entity.TrdProjectKpi;
import com.sz.biz.common.project.service.TrdProjectKpiService;
import com.sz.common.base.dto.QResultDto;
import com.sz.common.base.dto.QueryParams;
import com.sz.common.base.dto.ResultDto;
import com.sz.common.base.exception.Exceptions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Function: TODO: ADD FUNCTION <br>
 * 贸易关键指标Controller
 *
 * @author fengwj <br>
 * Date: 2017-12-11 15:31:00
 */
@RestController
@RequestMapping("${api}/biz/trade/projectkpis")
@Api(description = " ", tags = "062、贸易关键指标管理")
public class TrdProjectKpiController extends BaseController {

    /**
     * service接口
     */
    @Autowired
    private TrdProjectKpiService trdProjectKpiService;

    /**
     * 查询列表
     *
     * @param pageIndex 分页
     * @param pageSize  每页条数
     * @param sort      排序的字段
     * @param asc       排序方式
     * @return 查询分页结果
     * @author Fengwj
     * @date 17:47 2017/12/13
     */
    @ApiOperation(value = "贸易关键指标所有列表", notes = "贸易关键指标所有列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "分页", dataType = "int", paramType = "query", defaultValue
                    = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数,max<2000", dataType = "int", paramType = "query",
                    defaultValue = "10"),
            @ApiImplicitParam(name = "sort", value = "排序的字段", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "asc", value = "排序方式", dataType = "boolean", paramType = "query", defaultValue =
                    "true")
    })
    @RequestMapping(value = "", method = {RequestMethod.GET})
    public QResultDto getTrdProjectkpiList(@RequestParam(required = false, defaultValue = "1") int pageIndex,
                                           @RequestParam(required = false, defaultValue = "10") int pageSize,
                                           @RequestParam(required = false) String sort,
                                           @RequestParam(required = false, defaultValue = "true") Boolean asc) {
        QResultDto qResultDto = trdProjectKpiService.getTrdProjectkpiList(new QueryParams(pageSize, pageIndex, null, sort, asc));
        return qResultDto;
    }

    /**
     * 查询最新添加的3条数据列表
     * @return 查询结果
     * @author Fengwj
     * @date 17:48 2017/12/13
     */
    @ApiOperation(value = "最新创建列表", notes = "最新创建列表")
    @RequestMapping(value = "/newest", method = {RequestMethod.GET})
    public ResultDto getNewestList() {
        ResultDto resultDto = trdProjectKpiService.getNewestList();
        return resultDto;
    }

    /**
     * 按id查询
     * @param id 主键
     * @return 查询结果
     * @author Fengwj
     * @date 17:49 2017/12/13
     */
    @ApiOperation(value = "按id查找", notes = "按id查找")
    @ApiImplicitParam(name = "id", value = "数据主键id", required = true, dataType = "Integer", paramType = "path")
    @RequestMapping(value = "/{id}", method = {RequestMethod.GET})
    public ResultDto findById(@PathVariable Integer id) {
        ResultDto resultDto = new ResultDto();
        resultDto.setData(trdProjectKpiService.findById(id));
        return resultDto;
    }

    /**
     * 新建
     * @param trdProjectKpiDto 新建参数
     * @return 新建成功返回id
     * @author Fengwj
     * @date 17:50 2017/12/13
     */
    @ApiOperation(value = "新建", notes = "新建")
    @ApiImplicitParam(name = "trdProjectKpiDto", value = "保存的数据\r\n" +
            "projectName(项目名称),annualizedReturn(年化收益率),turnoverDay(周转天数)," +
            "overdueRate(逾期率)", required = true, dataType = "TrdProjectKpiDto", paramType = "body")
    @RequestMapping(value = "", method = {RequestMethod.POST})
    public ResultDto save(@RequestBody TrdProjectKpiDto trdProjectKpiDto) {
        int id = trdProjectKpiService.save(trdProjectKpiDto);
        ResultDto resultDto = getSaveSuccessDto(MessagesCodes.MODULE_TRADE_PROJECT_KPI);
        resultDto.setData(id);
        return resultDto;
    }

    /**
     * 修改
     * @param trdProjectKpiDto 修改参数
     * @return 修改成功返回id
     * @author Fengwj
     * @date 17:50 2017/12/13
     */
    @ApiOperation(value = "修改", notes = "修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "数据主键id", required = true, dataType = "Integer", paramType = "path"),
            @ApiImplicitParam(name = "trdProjectKpiDto", value = "修改的数据\r\n" +
                    "id(主键),projectName(项目名称),annualizedReturn(年化收益率),turnoverDay(周转天数)," +
                    "overdueRate(逾期率)", required = true, dataType = "TrdProjectKpiDto", paramType = "body")
    })
    @RequestMapping(value = "/{id}", method = {RequestMethod.POST})
    public ResultDto update(@PathVariable Integer id, @RequestBody TrdProjectKpiDto trdProjectKpiDto) {
        //修改数据未找到抛出异常
        TrdProjectKpi trdProjectKpi = trdProjectKpiService.findById(id);
        if (null == trdProjectKpi) {
            throw Exceptions.bizException(ErrorCodes.ERROR_MODEL_NOT_FOUND, id);
        }
        ResultDto resultDto = getUpdateSuccessDto(MessagesCodes.MODULE_TRADE_PROJECT_KPI);
        resultDto.setData(trdProjectKpiService.update(trdProjectKpiDto));
        return resultDto;
    }

    /**
     * 按id主键集合假删除数据
     * @param ids id主键集合
     * @return 删除成功提示
     * @author Fengwj
     * @date 17:52 2017/12/13
     */
    @ApiOperation(value = "按id删除", notes = "按id删除")
    @ApiImplicitParam(name = "ids", value = "数据主键id集合", required = true, dataType = "string", paramType = "body")
    @RequestMapping(value = "/deletions", method = {RequestMethod.POST})
    public ResultDto deleteById(@RequestBody List<Integer> ids) {
        trdProjectKpiService.batchDelete(ids);
        ResultDto resultDto = getDeleteSuccessDto(MessagesCodes.MODULE_TRADE_PROJECT_KPI);
        return resultDto;
    }

}
